package cn.com.lsjz.nursemanager.repository.system;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.com.lsjz.commons.repository.base.BaseRepository;
import cn.com.lsjz.nursemanager.model.system.SysOperation;


public interface SysOperationRepository extends BaseRepository<SysOperation, Integer> {

	/**
	 * 根据用户ID获取对应的菜单信息
	 * 
	 * @param userId
	 * @return
	 */
	@Query(value="SELECT a.* FROM SYS_OPERATIONS a WHERE a.operation_Id IN "
			+ "(SELECT DISTINCT b.p_Operation_Id FROM SYS_OPERATIONS b WHERE b.operation_Id IN "
			+ "(SELECT m.operation_Id FROM SYS_OPERATIONS m,sys_role_vs_operation rm  "
			+ "WHERE m.operation_Id = rm.operation_Id  AND rm.role_Id IN "
			+ "(SELECT ur.role_Id FROM sys_User_Vs_Role ur WHERE ur.user_Id = :userId)))"
			+ " UNION ALL "
			+ " SELECT m.* FROM SYS_OPERATIONS m,sys_role_vs_operation rm "
			+ "WHERE m.operation_Id = rm.operation_Id  AND rm.role_Id IN "
			+ "(SELECT ur.role_Id FROM sys_User_Vs_Role ur WHERE ur.user_Id = :userId) "
			,nativeQuery=true)
	List<SysOperation> loadByUserId(@Param("userId") String userId);

	/**
	 * 获取所有的父级菜单
	 * 
	 * @return
	 */
	@Query(" select m from SysOperation m where m.parentOperationId = -1 order by m.sortIndex")
	List<SysOperation> loadParentModule();

	/**
	 * 根据父级菜单编号查询
	 * 
	 * @param parentOperationId
	 * @return
	 */
	List<SysOperation> getByParentOperationIdOrderBySortIndex(Integer parentOperationId);

}
